#include "CProgramVPS.h"
#include "ILDXException.h"

ldx::render::CProgramVPS::CProgramVPS(SDeviceMap dm)
{
	DM = dm;
}

ldx::render::CProgramVPS::~CProgramVPS()
{
	if (VertexShader)
		VertexShader->Release();
	if (PixelShader)
		PixelShader->Release();
}

void ldx::render::CProgramVPS::set(SShaderMid mid)
{
	switch (mid.Type)
	{
	case EST_VERTEXSHADER:
		if (mid.InputLayout != nullptr)
			InputLayout = mid.InputLayout;
		VertexShader = mid.VertexShader;
		break;
	case EST_PIXELSHADER:
		PixelShader = mid.PixelShader;
		break;
	default:
		throw os::ILDXException(L"CProgramVPS::set valid shader type");
		break;
	}
}

void ldx::render::CProgramVPS::use()
{
	assert(VertexShader != nullptr&&PixelShader != nullptr);
	if (InputLayout)
		DM.Window.D3DDeviceContext->IASetInputLayout(InputLayout);
	DM.Window.D3DDeviceContext->VSSetShader(VertexShader, NULL, 0);
	DM.Window.D3DDeviceContext->PSSetShader(PixelShader, NULL, 0);
}

void ldx::render::CProgramVPS::stopUsing()
{
}

void ldx::render::CProgramVPS::setConstantBuffers(E_SHADER_TYPE type, uint startSlot, uint numBuffer, CBufferObject & bufferObjet)
{
	assert(VertexShader != nullptr&&PixelShader != nullptr);
	switch (type)
	{
	case ldx::render::EST_VERTEXSHADER:
		DM.Window.D3DDeviceContext->VSSetConstantBuffers(startSlot, numBuffer, bufferObjet.getBufferObject());
		break;
	case ldx::render::EST_PIXELSHADER:
		DM.Window.D3DDeviceContext->PSSetConstantBuffers(startSlot, numBuffer, bufferObjet.getBufferObject());
		break;
	default:
		break;
	}

}

void ldx::render::CProgramVPS::setVertexConstantBuffers(uint startSlot, uint numBuffer, CBufferObject & bufferObjet)
{
	assert(VertexShader != nullptr&&PixelShader != nullptr);
	DM.Window.D3DDeviceContext->VSSetConstantBuffers(startSlot, numBuffer, bufferObjet.getBufferObject());
}

void ldx::render::CProgramVPS::setPixelConstantBuffers(uint startSlot, uint numBuffer, CBufferObject & bufferObjet)
{
	assert(VertexShader != nullptr&&PixelShader != nullptr);
	DM.Window.D3DDeviceContext->PSSetConstantBuffers(startSlot, numBuffer, bufferObjet.getBufferObject());
}
